<?php  
include("common/db.php");
if($_SESSION['userid']==""){
  echo "<script>window.location.href='login.php';</script>";
}
// 获取时间
if (isset($_REQUEST[date])) {
  $date=$_REQUEST[date];
  $date=date("Y-m-d",strtotime($date));
  $latedate=date("Y-m-d",(strtotime($date) + 3600*24));
}else{
  $date=date("Y-m-d");
  $latedate=date("Y-m-d",(strtotime($date) + 3600*24));
}
// 获取时间 end
 

// 打款
if ($_GET[act]=="dakuan") {
  $sql="select * from brokerage where shopid=".$_GET[shopid]." and date='".$_GET[date]."'";
  $brokerage_row=$mysql->getRow($sql);
  $dakuan_state=1;
  if (!$brokerage_row) {
    $sql="INSERT INTO brokerage (date,shopid,dakuan_state) value ('".$_GET[date]."',".$_GET[shopid].",".$dakuan_state.")";
    $mysql->query($sql);
// 判断打款是否成功
    if ($mysql->affected_rows()>0) {
// 改变平台资产及收益
      $sql="UPDATE admin set property=property-".$_GET[turnover].",income=income+".$_GET[brokerage]."";
      $mysql->query($sql);
// 记录现金流量
      $sql="select name from shop where shopid=".$_GET[shopid]."";
      $shopname=$mysql->getOne($sql);
      $content="平台打款".($_GET[turnover]-$_GET[brokerage])."元给".$shopname;
      $sql="INSERT INTO cash_flow (amount,fromshopid,content) value (".-($_GET[turnover]-$_GET[brokerage]).",".$_GET[shopid].",'".$content."')";
      $mysql->query($sql);
      $content="平台打款".($_GET[turnover]-$_GET[brokerage])."元给".$shopname."获取佣金提成".$_GET[brokerage]."元";
      $sql="INSERT INTO cash_flow (amount,fromshopid,content) value (".$_GET[brokerage].",".$_GET[shopid].",'".$content."')";
      $mysql->query($sql);
// 记录现金流量 end
// 改变平台资产及收益 end
// 改变商家余额
      $sql="UPDATE shop set balance=balance-".($_GET[turnover]-$_GET[brokerage])." where shopid=".$_GET[shopid]."";
      $mysql->query($sql);
// 记录现金流量
      $fromshopid=0;
      $content="获得平台打款".($_GET[turnover]-$_GET[brokerage])."元到".$shopname."账户";
      $sql="INSERT INTO cash_flow (shopid,amount,fromshopid,content) value (".$_GET[shopid].",".-($_GET[turnover]-$_GET[brokerage]).",".$fromshopid.",'".$content."')";
      $mysql->query($sql);
// 记录现金流量 end
// 改变商家余额 end
      echo "<script>alert('打款成功!');window.location.href='brokerage.php?date=".$_GET[date]."'</script>";
    }else{
      echo "<script>alert('打款失败!');window.location.href='brokerage.php?date=".$_GET[date]."'</script>";
    }
// 判断打款是否成功 end
  }
}
// 打款 end


// 查找商品信息
$sql="select * from shop";
$shop=$mysql->getAll($sql);
// 查找商品信息 end
// 计算营业额及佣金 
$brokerage=array();
$turnover = array();
foreach ($shop as $v) {
  $sql="select * from goodsorders where shopid=".$v[shopid]." and addtime<'".$latedate."' and addtime>'".$date."' and state>0";
  $orders=$mysql->getAll($sql);
// 查找返回率
  $sql="select rates from categroy_type where id=(select categroy_type_id from categroy where cateid='".$v[leimu]."')";
  $rates=$mysql->getOne($sql);
// 查找返回率 end
  $turnover[$v[shopid]]=0;
  foreach ($orders as $v1) {
    $turnover[$v[shopid]]+=$v1[totalprice];
  }

  foreach ($orders as $v1) {
// 查找brokerage表中的打款和返还状态
    $sql="select * from brokerage where shopid=".$v[shopid]." and date='".$date."'";
    $prebrokerage=$mysql->getRow($sql);
    if (!$prebrokerage) {
      $brokerage[$v[shopid]][dakuan_state]=0;
    }else{
      $brokerage[$v[shopid]][dakuan_state]=$prebrokerage[dakuan_state];
    }
// 查找brokerage表中的打款和返还状态 end
    $brokerage[$v[shopid]][turnover]=$turnover[$v[shopid]];
    $brokerage[$v[shopid]][brokerage]=$turnover[$v[shopid]]*$rates*0.01;
    $brokerage[$v[shopid]][shopname]=$v[name];
    $brokerage[$v[shopid]][rates]=$rates;
    $brokerage[$v[shopid]][balance]=$v[balance];
  }
}
// 计算营业额及佣金 end
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <title>e网批发管理后台</title>

  <script type="text/javascript" src="js/jquery.min.js"></script>
  <link type="text/css" rel="stylesheet" href="time/css/lanrenzhijia.css" />
  <link rel="stylesheet" type="text/css" href="time/css/jquery-ui.css" />
  <script src="time/js/jquery-ui-1.10.4.custom.min.js"></script>
  <script src="time/js/jquery.ui.datepicker-zh-CN.js"></script>
  <script src="time/js/jquery-ui-timepicker-addon.js"></script>
  <script src="time/js/jquery-ui-timepicker-zh-CN.js"></script>
  <link href="css/styles.css" rel="stylesheet" type="text/css" />
  <link href="css/style2.css" rel="stylesheet" type="text/css" />
  <link rel="stylesheet" type="text/css" href="css/brokerage.css">
  <script language="javascript" type="text/javascript" src="datepicker/WdatePicker.js"></script>

</head>

<body>

  <!--header-->
  <?php include("common/header.php");?>
  <!--header end-->


  <!-- staqrt subnav -->
  <div id="sub_nav">

    <div id="subnav_wrap" class="container_12">

      <!-- start sub nav list -->
      <div id="subnav" class=" grid_12">

        <ul>
          <li><a href="shequ.php" >商家管理</a></li>
          <li><a href="brokerage.php" class="sub_nav_active" >佣金管理</a></li>
        </ul>

      </div>

    </div>


  </div>
  <div class="container_12">
    <form action="" method="post">
      <span>开始时间</span>
      <input name="date" type="text" value="<?php echo $date; ?>" style="cursor:pointer;" onClick="WdatePicker()"/>
      &nbsp;&nbsp;&nbsp;&nbsp;
      <span>结束时间</span>
      <input name="date" type="text" value="<?php echo $date; ?>" style="cursor:pointer;" onClick="WdatePicker()"/>
      <input type="submit" value="提交" class="submit">
    </form>
  </div>

  <div class="container_12">

    <table width="100%" border="0" cellpadding="0" cellspacing="0">



      <tr>
        <th width="10%" scope="col">时间</th>
        <th width="10%" scope="col">商家名称</th>
        <th width="20%" scope="col">商家余额</th>
        <th width="20%" scope="col">营业额</th>
        <th width="10%" scope="col">返还金额</th>
        <th width="10%" scope="col">打款状态</th>
        <th width="30%" scope="col">操作<span style="color:red;">(营业额=返给平台佣金+商家收款金额)</span></th>
      </tr>

      <?php foreach ($brokerage as $k=>$v) { ?>
      <tr>

        <td scope="col"><?php echo $date;?></td>
        <td scope="col"><?php echo $v[shopname];?></td>
        <td scope="col"><?php echo $v[balance];?>元
          <span class="cash_flow_a">(<a href="cash_flow.php?act=1&shopid=<?php echo $k; ?>" value="<?php echo $k; ?>">查看流水</a>)</span></td>
          <td scope="col"><?php echo $v[turnover];?>元
            <span class="cash_flow_a">(<a href="cash_flow.php?act=0&shopid=<?php echo $k; ?>&date=<?php echo $date; ?>&latedate=<?php echo $latedate; ?>" value="<?php echo $k; ?>">查看流水</a>)</span>
          </td>
          <td scope="col"><?php echo $v[brokerage];?>元
            <span>(<?php echo $v[rates]; ?>%)</span></td>
            <td scope="col">
              <?php
              if ($v[dakuan_state]==0) {
                echo "未打款";
              }elseif($v[dakuan_state]==1){
                echo "已打款";
              }
              ?>
            </td>
            <td scope="col">
              <?php if ($v[dakuan_state]==0) { ?>
              <div class="youse">
                <a style="display:inline-block;width:200px;" href="brokerage.php?act=dakuan&shopid=<?php echo $k; ?>&date=<?php echo $date; ?>&turnover=<?php echo $v[turnover]; ?>&brokerage=<?php echo $v[brokerage]; ?>">
                  <span class="colorw">打款给商家(自动返佣)</span>
                </a>
              </div>
              <?php } ?>
            </td>
          </tr>
          <?php } ?>		
        </table>
        <div class="grid_12">
          <ul id="pagination">






          </ul>

        </div>           
      </div>
    </body>
    </html>
